<template>
  <div class="login-page">
    <el-card>
      <template #header>黑马面经运营后台</template>
      <el-form ref="loginForm" autocomplete="off" :model="formData" :rules="rules">
        <el-form-item label="用户名"  prop="username">
          <el-input v-model="formData.username" placeholder="输入用户名"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" type="password" placeholder="输入用户密码"></el-input>
        </el-form-item>

        <el-form-item class="tc">
          <el-button type="primary" @click="doLogin">登 录</el-button>
          <el-button @click="reset">重 置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'login-page',
  data () {
    return {
      formData: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          { required: true, message: '公主请输入用户名 ', trigger: 'blur' },
          { min: 4, max: 6, message: '长度在 4 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '公主请输入密码', trigger: 'blur' },
          { min: 4, max: 6, message: '长度在 4 到 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async doLogin () {
      // 1.表单校验
      await this.$refs.loginForm.validate()
      // 2.发请求==？触发actions
      await this.$store.dispatch('user/login', this.formData)
      this.$message.success('登录成功，已跳转')
      this.$router.push('/')
    },
    // 重置表单，并且移除所有的错误信息
    reset () {
      this.$refs.loginForm.resetFields()
    }
  }

}

</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;

  .el-card {
    width: 420px;

    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }

  .el-form {
    padding: 0 20px;
  }

  .tc {
    text-align: center;
  }
}</style>
